package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Category;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.acl.LastOwnerException;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //查询商品信息
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "key", required = false) String key
    ) {
        //返回前端的分页数据是SpuBo对象的
        PageResult<SpuBo> pageResult = this.goodsService.querySpuByPageAndSort(page, rows, saleable, key);

        if (pageResult == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(pageResult);
    }

    //新增商品信息
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        try {
            this.goodsService.saveGoods(spuBo);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //修改商品之回显spuDetail
    @GetMapping("spu/detail/{id}")
    public ResponseEntity<SpuDetail> querySpuDetailById(@PathVariable("id") Long id) {
        SpuDetail spuDetail = this.goodsService.querySpuDetailById(id);
        if (spuDetail == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(spuDetail);
    }

    //修改商品之回显sku和stock（库存）
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuById(@RequestParam("id") Long id) {
        List<Sku> list = this.goodsService.querySkuById(id);
        if (list == null || list.size() < 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    //提交修改商品信息请求
    @PutMapping("goods")
    //接收json格式的请求参数
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo) {
        try {
            this.goodsService.updateGoods(spuBo);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //修改spu的为下架
    @PutMapping("spu/soldout/{spuid}")
    public ResponseEntity<Void> soldout(@PathVariable("spuid") Long spuid) {
        int i = this.goodsService.soldout(spuid);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    //修改spu的为上架
    @PutMapping("spu/onsale/{spuid}")
    public ResponseEntity<Void> onsale(@PathVariable("spuid") Long spuid) {
        int i = this.goodsService.onsale(spuid);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    //删除商品
    @DeleteMapping("spu/delGoods/{spuid}")
    public ResponseEntity<Void> delGoods(@PathVariable("spuid") Long spuid) {
        int i = this.goodsService.delGoods(spuid);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    //根据spuid查询spu的信息
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id) {
        Spu spu = this.goodsService.querySpuById(id);
        if (spu == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(spu);
    }

    //根据skutitle查询sku的信息
    @PostMapping("sku/newList")
    public ResponseEntity<List<Sku>> querySkuBySkuId(@RequestBody List<String> skutitles) {
        List<Sku> list = this.goodsService.querySkuBySkuId(skutitles);
        if (null == list || list.size() < 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    //根据前台传过来的skutitle来获取sku信息，再通过sku中包含的spuid来查询相对应的spu
    //根据spu中的saleable的值，来判断该商品是否处于下架状态
    @PostMapping("spu/oldSpu")
    public ResponseEntity<List<Map<String,Object>>> querySpuBySku(@RequestBody List<String> skutitles) {
        List<Map<String,Object>> list = this.goodsService.querySpuBySku(skutitles);
        if (null == list || list.size() < 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }
}
